﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DisplayBoardInfo.Server.Entities
{
    public class Users
    {
        #region Factory methods

        /// <summary>
        /// Gets all users.
        /// </summary>
        /// <returns>Collection of all users.</returns>
        public static IEnumerable<User> GetAll()
        {
            List<User> result = new List<User>();
            Model.DatabaseContextDataContext db = new Model.DatabaseContextDataContext();
            foreach (Model.User item in db.Users)
            {
                result.Add(new User(item.Username));
            }

            return result;
        }

        /// <summary>
        /// Gets user with spcyfic username.
        /// </summary>
        /// <param name="username">Username</param>
        /// <returns>Single instance of user with specyfic username.</returns>
        /// <exception cref="IndexOutOfRangeException"></exception>
        public static User GetByUsername(string username)
        {
            if(IsUserExists(username)) return new User(username);
            else throw new IndexOutOfRangeException("User is not exists.");
        }

        #endregion

        #region Helper methods

        /// <summary>
        /// Checks that specyfic user already exists.
        /// </summary>
        /// <param name="username">Username</param>
        /// <returns>Returns true if user exists or false otherwise.</returns>
        public static bool IsUserExists(string username)
        {
            Model.DatabaseContextDataContext db = new Model.DatabaseContextDataContext();
            return db.Users.Where(i => i.Username == username).Count() > 0;
        }

        #endregion
    }
}
